<?php 
class server_admin 
{ 
	function admin_server_list() 
	{ 
		global $mysql_conn,$v_file;
		$list_page = $_GET['list_page'];
		$list_num = $_GET['list_num'];
		$sousuokey = $_GET['sousuokey'];
		if (empty($list_page)) $list_page = 1;
		if (empty($list_num)) $list_num = 10;
		if ($list_page < 5) {$list_page0 = 1;} else {$list_page0 = $list_page-4;}
		if ($list_page < 5) {$list_page1 = 1;} else {$list_page1 = $list_page-3;}
		if ($list_page < 5) {$list_page2 = 2;} else {$list_page2 = $list_page-2;}
		if ($list_page < 5) {$list_page3 = 3;} else {$list_page3 = $list_page-1;}
		if ($list_page < 5) {$list_page4 = 4;} else {$list_page4 = $list_page;}
		if ($list_page < 5) {$list_page5 = 5;} else {$list_page5 = $list_page+1;}
		if ($list_page < 5) {$list_page6 = 6;} else {$list_page6 = $list_page+2;}
		if ($list_page < 5) {$list_page7 = 7;} else {$list_page7 = $list_page+3;}
		if ($list_page < 5) {$list_page8 = 8;} else {$list_page8 = $list_page+4;}
		$page_btn .= "<button type='button' class='btn btn-white' onClick='list_page_go(".$list_page0.")'><i class='fa fa-chevron-left'></i></button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page1."' onClick='list_page_go(".$list_page1.")'>".$list_page1."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page2."' onClick='list_page_go(".$list_page2.")'>".$list_page2."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page3."' onClick='list_page_go(".$list_page3.")'>".$list_page3."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page4."' onClick='list_page_go(".$list_page4.")'>".$list_page4."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page5."' onClick='list_page_go(".$list_page5.")'>".$list_page5."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page6."' onClick='list_page_go(".$list_page6.")'>".$list_page6."</button>";
		$page_btn .= "<button class='btn btn-white' id='list_page".$list_page7."' onClick='list_page_go(".$list_page7.")'>".$list_page7."</button>";
		$page_btn .= "<button type='button' class='btn btn-white' onClick='list_page_go(".$list_page8.")'><i class='fa fa-chevron-right'></i></button>";
		$list_limit = ($list_page-1)*$list_num.",".$list_num;
		if (empty($sousuokey)) {
			$sql = "SELECT * from `server` limit ".$list_limit;
		} else {
			$sql = "SELECT * from `server` WHERE `server_name` = '".$sousuokey."'";
		}
		$result = mysql_query($sql,$mysql_conn);
		while(@$row=mysql_fetch_array($result)) {
			$server_id = $row['server_id'];
			$server_name = $row['server_name'];
			$server_url = $row['server_url'];
			$server_state_url = $server_url."openwx/check_client"; 
			@$html = file_get_contents($server_state_url);
			if(!empty($html)) {
				$server_state = "服务器运行中！";
			} else {
				$server_state = "服务器已关闭！";
			}
			$serverlist .= "<tr><td>";
			$serverlist .= "    <input type='checkbox' class='i-checks' name='input[]'>";
			$serverlist .= "</td>";
			$serverlist .= "<td>".$server_id."</td>";
			$serverlist .= "<td>".$server_name."</td>";
			$serverlist .= "<td>".$server_url."</td>";
			$serverlist .= "<td>".$server_state."</td>";
			$serverlist .= "<td>";
			$serverlist .= "<a href='#' class='btn btn-white btn-xs' onClick='admin_server_edit(".$server_id.");'><i class='fa fa-edit'></i> 编辑 </a>";
			$serverlist .= "<a href='#' class='btn btn-white btn-xs' onClick='admin_server_del(".$server_id.");'><i class='fa fa-trash-o'></i> 删除 </a>";
			$serverlist .= "</td></tr>";
		}
		require($v_file);
	}
	function admin_server_add() 
	{ 
		global $mysql_conn,$v_file;
		require($v_file);
	}
	function admin_server_add_do() 
	{ 
		global $mysql_conn;
		$server_name = $_POST['server_name'];
		$server_url = $_POST['server_url'];
		$sql = "SELECT * from `server` WHERE `server_name` = '".$server_name."'";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		if (empty($row)) {
			$sql="INSERT INTO server (server_id, server_name, server_url) VALUES (NULL,'$server_name','$server_url')";
			$result = mysql_query($sql,$mysql_conn);
			@$row = mysql_fetch_array($result);
			echo "2";//服务器添加成功
		} else {
			echo  "1";//服务器名已存在
		}
	}
	function admin_server_edit() 
	{ 
		global $mysql_conn,$v_file;
		$server_id = $_GET['server_id'];
		$sql = "SELECT * from `server` WHERE `server_id` = '".$server_id."'";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		$server_name = $row['server_name'];
		$server_url = $row['server_url'];
		require($v_file);
	}
	function admin_server_edit_do() 
	{ 
		global $mysql_conn;
		$server_id = $_POST['server_id'];
		$server_name = $_POST['server_name'];
		$server_url = $_POST['server_url'];
		$sql = "UPDATE `server` SET `server_name` = '".$server_name."', `server_url` = '".$server_url."' WHERE `server_id` ='".$server_id."';";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		echo "2";
	}
	function admin_server_del_do() 
	{ 
		global $mysql_conn;
		$server_id = $_POST['server_id'];
		$sql = "DELETE from `server` WHERE `server_id` = '".$server_id."'";
		$result = mysql_query($sql,$mysql_conn);
		@$row = mysql_fetch_array($result);
		echo "2";
	}
} 
/* End of file democontroller.php */ 